﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class ArticleDAL
    {
        SQLHelper SQLHelper = null;
        QXHelper QXHelper = null;
        public ArticleDAL()
        {
            SQLHelper = new SQLHelper();
            QXHelper = new QXHelper();
        }


        #region 显示文章分类
        public string jhy_category_query()
        {
            DataTable dt = new DataTable();
            string proc = "jhy_category_query";
            dt = SQLHelper.ExecuteQuery(proc, CommandType.StoredProcedure);
            return QXHelper.LayuiTableJson(dt);
        }
        #endregion
        #region 添加文章分类
        public bool jhy_category_add(string cate_name)
        {
            bool flag = false;
            string proc = "jhy_category_add";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@cate_name",cate_name)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        #region 编辑文章分类
        public bool jhy_category_edit(string cate_id, string cate_name)
        {
            bool flag = false;
            string proc = "jhy_category_edit";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@cate_id",cate_id),
                new SqlParameter("@cate_name",cate_name)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        #region 删除文章分类
        public bool jhy_category_delete(string cate_id)
        {
            bool flag = false;
            string proc = "jhy_category_delete";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@cate_id",cate_id)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion


        #region 发布文章
        public string jhy_article_post(string art_title,string art_intro, string art_content,string art_state,string art_userid, string art_cateid)
        {
            DataTable dt = new DataTable();
            string proc = "jhy_article_post";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@art_title",art_title),
                new SqlParameter("@art_intro",art_intro),
                new SqlParameter("@art_content",art_content),
                new SqlParameter("@art_state",art_state),
                new SqlParameter("@art_userid",art_userid),
                new SqlParameter("@art_cateid",art_cateid)
            };
            dt = SQLHelper.ExecuteQuery(proc, spa, CommandType.StoredProcedure);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            else
            {
                return "false";
            }
        }

        #endregion
        #region 修改文章
        public bool jhy_article_edit(string art_id, string art_title,string art_intro, string art_content, string art_state, string art_cateid)
        {
            bool flag = false;
            string proc = "jhy_article_edit";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@art_id",art_id),
                new SqlParameter("@art_title",art_title),
                new SqlParameter("@art_intro",art_intro),
                new SqlParameter("@art_content",art_content),
                new SqlParameter("@art_cateid",art_cateid),
                new SqlParameter("@art_state",art_state)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        #region 删除文章
        public bool jhy_article_delete(string art_id)
        {
            bool flag = false;
            string proc = "jhy_article_delete";            
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@art_id",art_id)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        #region 文章列表
        public string jhy_article_query(string art_userid,string art_cateid, string art_title, string laypage, string laylimit)
        {
            DataSet ds = new DataSet();
            string proc = "jhy_article_query";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@user_id",art_userid),
                new SqlParameter("@art_cateid",art_cateid),
                new SqlParameter("@art_title",art_title),
                new SqlParameter("@laypage",laypage),
                new SqlParameter("@laylimit",laylimit)
            };
            ds = SQLHelper.ExecuteQueryDataSet(proc, spa, CommandType.StoredProcedure);
            return QXHelper.LayuiTablePageJson(ds);
        }
        #endregion
        #region 加载要编辑的文章(未使用)
        public DataTable jhy_article_edit_load(string art_id)
        {
            DataTable dt = new DataTable();
            string proc = "jhy_article_edit_load";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@art_id",art_id)
            };
            dt = SQLHelper.ExecuteQuery(proc, spa, CommandType.StoredProcedure);
            return dt;
        }
        #endregion
        #region 获取文章分类
        public DataTable jhy_category_select_query()
        {
            DataTable dt = new DataTable();
            string proc = "jhy_category_query";
            dt = SQLHelper.ExecuteQuery(proc, CommandType.StoredProcedure);
            return dt;
        }
        #endregion


        #region 首页flow
        public string jhy_index_flow(string art_title,string laypage)
        {
            DataSet ds = new DataSet();
            string proc = "jhy_index_flow";
            SqlParameter[] spa = new SqlParameter[] {               
                new SqlParameter("@art_title",art_title),
                new SqlParameter("@laypage",laypage)
            };
            ds = SQLHelper.ExecuteQueryDataSet(proc, spa, CommandType.StoredProcedure);
            return QXHelper.LayuiTablePageJson(ds);
        }
        #endregion
        #region details页显示文章
        public string jhy_index_details(string art_id)
        {
            DataTable dt = new DataTable();
            string proc = "jhy_index_details";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@art_id",art_id)
            };
            dt = SQLHelper.ExecuteQuery(proc, spa, CommandType.StoredProcedure);
            return QXHelper.TableToJson(dt);
        }
        #endregion





    }
}
